/**
 * @(#)ApkConfigController.java 2017年03月1日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.jsbd.boss.domian.BookCompany;
import org.jsbd.boss.service.IBookCompanyService;
import org.jsbd.boss.common.DataGridModel;
import org.jsbd.boss.common.ResponseMapUtils;
import org.jsbd.boss.common.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.dubbo.common.utils.StringUtils;

@Controller
@RequestMapping("/bookCompany")
public class BookCompanyController {

	public static final Logger logger = LoggerFactory.getLogger(BookCompanyController.class);

	@Autowired
	private IBookCompanyService bookCompanyService;

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		return "bookCompany/list";
	}

	@RequestMapping(value = "save", method = RequestMethod.POST)
	@ResponseBody
	public Object save(BookCompany bookCompany) {
		if (StringUtils.isBlank(bookCompany.getName())) {
			return ResponseMapUtils.responseFail("名称不能为空");
		}
		bookCompanyService.save(bookCompany);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public Object update(BookCompany bookCompany) {
		if (bookCompany.getId() == null) {
			return ResponseMapUtils.responseFail("参数缺失，请确认参数再提交");
		}
		BookCompany obj = bookCompanyService.getById(bookCompany.getId());
		obj.setName(bookCompany.getName());
		obj.setBookGroupId(bookCompany.getBookGroupId());
		obj.setStatus(bookCompany.getStatus());
		bookCompanyService.update(obj);
		return ResponseMapUtils.responseSuccess();
	}

	@RequestMapping(value = "getListPage")
	@ResponseBody
	public Map<String, Object> getListPage(DataGridModel page, BookCompany bookCompany) throws Exception {
		return bookCompanyService.getListPage(page, bookCompany);
	}

	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public void delete(Integer[] ids, HttpServletResponse response) {
		try {
			List<Integer> list = Arrays.asList(ids);
			bookCompanyService.delete(list);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			ResponseUtils.responseFailure(response);
		}
	}

}
